package com.dotndot.momserver.xml;

import java.io.*;
import java.util.*;

import javax.servlet.http.*;

import com.dotndot.momserver.data.*;

public class ResponseXML
{
    private static ResponseXML responseXML = null;

    public static ResponseXML get()
    {
        if( responseXML == null )
        {
            responseXML = new ResponseXML();
        }
        return responseXML;
    }

    public void makeCommonXml( HttpServletResponse resp, List<DataXml> response )
    {

        if( response.size() == 0 )
        {
            return;
        }

        XML.initList();
        for( DataXml xml : response )
        {
            if( xml.mType == XML.HEADER_TYPE )
            {
                XML.addHeaderList(xml);
            }
            else if( xml.mType == XML.DATA_TYPE )
            {
                XML.addDataList(xml);
            }
        }

        try
        {
            resp.setContentType("text/xml");
            resp.getWriter().println(XML.makeXml());
        }
        catch( IOException e )
        {
            e.printStackTrace();
        }
    }

    public void simpleRespList( RETURN.TYPE type, List<DataXml> list )
    {

        if( list == null )
        {
            return;
        }

        switch( type )
        {
            /* LOG TYPE */
            case RET_LOGIN_SUCCESS:
                list.add(new DataXml(XML.HEADER_TYPE, XML.CODE, "L001"));
                list.add(new DataXml(XML.HEADER_TYPE, XML.MESSAGE, "LOGIN SUCCESS"));
                break;

            case RET_JOIN_SUCCESS:
                list.add(new DataXml(XML.HEADER_TYPE, XML.CODE, "L002"));
                list.add(new DataXml(XML.HEADER_TYPE, XML.MESSAGE, "JOIN SUCCESS"));
                break;

            case RET_STORY_GET_LIST_SUCCESS:
                list.add(new DataXml(XML.HEADER_TYPE, XML.CODE, "L003"));
                list.add(new DataXml(XML.HEADER_TYPE, XML.MESSAGE, "STORY GET LIST SUCCESS"));
                break;

            case RET_STORY_ADD_ITEM_SUCCESS:
                list.add(new DataXml(XML.HEADER_TYPE, XML.CODE, "L004"));
                list.add(new DataXml(XML.HEADER_TYPE, XML.MESSAGE, "STORY ADD ITEM SUCCESS"));
                break;

            /* ERROR TYPE */
            case RET_LOGIN_EMAIL_FAIL:
                list.add(new DataXml(XML.HEADER_TYPE, XML.CODE, "E001"));
                list.add(new DataXml(XML.HEADER_TYPE, XML.MESSAGE, "WRONG ID"));
                break;

            case RET_LOGIN_PASSWORD_FAIL:
                list.add(new DataXml(XML.HEADER_TYPE, XML.CODE, "E002"));
                list.add(new DataXml(XML.HEADER_TYPE, XML.MESSAGE, "WRONG PASSWORD"));
                break;

            case RET_JOIN_EMAIL_UNAVAILABLE:
                list.add(new DataXml(XML.HEADER_TYPE, XML.CODE, "E003"));
                list.add(new DataXml(XML.HEADER_TYPE, XML.MESSAGE, "EMAIL UNAVAILABLE"));
                break;

            case RET_JOIN_EMAIL_DUPLICATED:
                list.add(new DataXml(XML.HEADER_TYPE, XML.CODE, "E004"));
                list.add(new DataXml(XML.HEADER_TYPE, XML.MESSAGE, "EMAIL DUPLICATED"));
                break;

            case RET_JOIN_PASSWORD_UNAVAILABLE:
                list.add(new DataXml(XML.HEADER_TYPE, XML.CODE, "E005"));
                list.add(new DataXml(XML.HEADER_TYPE, XML.MESSAGE, "PASSWORD UNAVAILABLE"));
                break;

        }
    }

    public void makeUserKey( Long key, List<DataXml> list )
    {
        list.add(new DataXml(XML.DATA_TYPE, XML.USER_KEY, key.toString()));
    }

//    public void makeStoryList( StoryItem item, List<DataXml> list )
//    {
//        list.add(new DataXml(XML.DATA_TYPE, XML.ITEM, XML.START));
//        list.add(new DataXml(XML.DATA_TYPE, XML.KEY, item.getKey().toString()));
//        if( item.getImage1() != null )
//        {
//            list.add(new DataXml(XML.DATA_TYPE, XML.IMAGE, XML.EXIST));
//        }
//        else
//        {
//            list.add(new DataXml(XML.DATA_TYPE, XML.IMAGE, XML.NOTHING));
//        }
//        if( item.getDescription() != null )
//        {
//            list.add(new DataXml(XML.DATA_TYPE, XML.DESCRIPTION, item.getDescription()));
//        }
//        list.add(new DataXml(XML.DATA_TYPE, XML.ITEM, XML.END));
//    }
}
